package com.lesson02;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Iterator;

//鼠标监听
public class TestMouse {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}
class MyFrame extends Frame {
    //属性
    private static ArrayList points;
    //需要画笔和鼠标的位置
    public MyFrame(String title){
        super(title);
        points = new ArrayList< >();
        setBounds(300,300,500,500);
        setVisible(true);
        //鼠标监听
        this.addMouseListener(new MyMouseListener());
    }
    @Override
    public void paint(Graphics g) {
        //画图与监听
        Iterator iterator = points.iterator();
        while (iterator.hasNext()){
            Point point = (Point) iterator.next();
            g.setColor(Color.BLACK);
            g.fillOval(point.x,point.y,10,10);
        }
    }
    private static class MyMouseListener extends MouseAdapter {
        @Override
        public void mouseClicked(MouseEvent e) {
            //鼠标点击
            MyFrame myFrame = (MyFrame) e.getSource();
            //点击的时候，在界面产生一个点
            points.add(e.getPoint());
            //每次点击都需要重画一遍
            myFrame.repaint();
        }
    }
}